﻿<?php

$con = mysql_connect("localhost","root","123") or die  ('Error connecting to mysql');
mysql_select_db("download.com.vn");
@mysql_query("SET NAMES 'UTF8'");
include("get_data_html.php");
include("seo.php");
/*

DROP TABLE IF EXISTS `ht4u_docx`;
CREATE TABLE `ht4u_docx` (
  `id` int(7) NOT NULL auto_increment,
  `idbv` int(10),
  `tenbv` longtext,
  `linkbv` longtext,
  `tag` longtext,
  `thumb` longtext,
  `buy` int(2) default '0',
  `linkdown` VARCHAR(1024) NOT NULL DEFAULT '0' ,
  `tthai` int(2) default '0',
  `idcate` int(5) default '0',
  `linkmf` longtext,
  `name_file` longtext,
  `size` varchar(20) default '-1',
  `ndbv` longtext,
  PRIMARY KEY  (`id`)
) ENGINE=MyISAM AUTO_INCREMENT=1 DEFAULT CHARSET=utf8;

//156,322
http://localhost/get-bv/get_code_docx.php?rs=7
http://localhost:9099/get-bv/idoc.vn/get_code_idoc.vn.php?rs=2&page=8541

//doc,docx,pdf,ppt,pptx


page: 10471
*/


//print_r($list);
global $get_data_html ;
global $seo ;

$get_data_html = new get_data_html();
$seo = new seo();

//get_detail();
//download_man();
//parse_man();
//insert_man();
//insert_lic();
//update_cateid();

//update_content_short();
//trans_post();
//update_cache();
//fix_detail();

//download_download();
//parse_linkdownload();
//parse_tag();

//trans_tag();
//fix_tag();
//parse_images_step1();
update_content();
function puts_downloadfile(){
$page = 0;
$limit = 20000;

$from = $page*$limit;
$to  = ($page+1)*$limit;

$result = mysql_query("SELECT `GET_ID`, `GET_LINK` FROM `getsoft` where GET_STATUS = 0");

while($row = mysql_fetch_array($result))
{
  //echo "\r title:".$row['SOFT_NAME'];
  echo "\rGet ID: ".$row['GET_ID'];
  
	$file = $row['GET_LINK'];
	// Open the file to get existing content
	$current = file_get_contents($file);

	// Write the contents back to the file
	file_put_contents('E:\file\\'.$row['GET_ID'].'.html', $current);
	mysql_query("update `getsoft` set GET_STATUS = 1 where GET_ID = ".$row['GET_ID']);
}

mysql_close($con);
}

function get_detail(){
$page = 0;
$limit = 10000;

$from = $page*$limit;
$to  = ($page+1)*$limit;
//$result = mysql_query("SELECT * FROM `getsoft` where GET_STATUS = 1 limit {$from},{$to}");
$result = mysql_query("SELECT GET_ID FROM `getsoft` where GET_STATUS = 1");
while($row = mysql_fetch_array($result))
{
	echo "\rGet ID: ".$row['GET_ID'];
	$link = 'E:\file\\'.$row['GET_ID'].'.html';
	$arr_insert = array();

	$arr_insert = parse_detail($link);
	
	echo ("UPDATE `getsoft` SET `SOFT_NAME_DESCRIPTION` = '".$arr_insert['SOFT_NAME_DESCRIPTION']."', `SOFT_CONTENT` = '".$arr_insert['SOFT_CONTENT']."', `SOFT_FILESIZE` = '".$arr_insert['SOFT_FILESIZE']."', `SOFT_TIME_CREATE` = ".$arr_insert['SOFT_TIME_CREATE'].", `SOFT_TIME_UPDATE` = ".$arr_insert['SOFT_TIME_UPDATE'].", `SOFT_THUMB` = '".$arr_insert['SOFT_THUMB']."', `SOFT_ICON` = '".$arr_insert['SOFT_ICON']."', `SOFT_VERSION` = '".$arr_insert['SOFT_VERSION']."', `SOFT_DOWN` = '".$arr_insert['SOFT_DOWN']."', `SOFT_STATUS` = 1, `SOFT_TIME_USE_LIMIT` = NULL, `SOFT_REQUIREMENT_OS` = '".$arr_insert['SOFT_REQUIREMENT_OS']."', `SEO_KEYWORD` = '".$arr_insert['SEO_KEYWORD']."', `SEO_DESCRIPTION` = '".$arr_insert['SEO_DESCRIPTION']."', `GET_DATA` = '".$arr_insert['GET_DATA']."', `GET_STATUS` = 2 WHERE `GET_ID` =  ".$row['GET_ID']."");
	usleep(50000);
	sleep(50000);
}

mysql_close($con);

}

function parse_detail($link = ''){
	global $get_data_html;
	global $seo;
	//$link = 'E:\file\1379.html';
	//echo '<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />';
	$html_content = $get_data_html->get_content($link);
	
	$man = $get_data_html->GetBetween($html_content, '<div class="publisher-softwares">','</div>');	
	$man = $get_data_html->GetBetween($man, 'href="','"');	
	$man = str_replace('Phát hành:','',$man);
	$man = trim(strip_tags($man));
	
	
	$lic = $get_data_html->GetBetween($html_content, '<li class="license-info clearfix">','</li>');	
	$lic = str_replace(array('<span class="item-info">','<span class="item-label">'),'',$lic);
	$lic = $get_data_html->GetBetween($lic, '<span class="','"');
	$lic = trim(strip_tags($lic));
	
	
	$cate = $get_data_html->GetBetween($html_content, '<div class="breadcrumb">','</div>');
	$array = array('type' => '1',
				'estart' => '<span',
				'eend' => '<\/span>',
			);
	$cate = $get_data_html->load($cate, $array);
	
	//$cate = $get_data_html->GetBetween($html_content, '<span typeof="v:Breadcrumb">','</span>');
	$cate = end($cate);
	$cate = $get_data_html->GetBetween($cate, 'href="','"');
	$cate = str_replace(array('Dung lượng:','<span class="item-label">'),'',$cate);
	$cate = trim(strip_tags($cate));
	$cate = 'http://www.download.com.vn'.$cate;
	
	
	$name_desc = $get_data_html->GetBetween($html_content, '<h1 class="detail-title">','</h1>');
	$name_desc = $get_data_html->GetBetween($name_desc, '<i>','</i>');
	$name_desc = trim(strip_tags($name_desc));
	
	
	$detail = $get_data_html->GetBetween($html_content, '<div class="content-overview">','<div class="clear"></div>');
	$detail = $get_data_html->removetag('<h3 class="title"><span id="IntroLabel">', '<\/h3>',$detail);
	$detail = $get_data_html->removetag('<div', '>',$detail);
	$detail = str_replace('</div>','',$detail);
	$detail = trim($detail);
	
	
	$size = $get_data_html->GetBetween($html_content, '<li class="filesize-info">','</li>');
	$size = str_replace(array('Dung lượng:','<span class="item-label">'),'',$size);
	$size = trim(strip_tags($size));
	
	$time_create = $get_data_html->GetBetween($html_content, '<li class="dateadded-info clearfix">','</li>');
	$time_create = $get_data_html->GetBetween($time_create, '<span class="item-info">','</span>');
	$time_create = trim(strip_tags($time_create));
	$time_create = explode("/",$time_create);
	$time_create = mktime(0, 0, $time_create[1], $time_create[0], $time_create[2]);

	
	$time_update = $get_data_html->GetBetween($html_content, '<span class="item-label">Ngày cập nhật:','</li>');
	$time_update = $get_data_html->GetBetween($time_update, '<span class="item-info">','</span>');
	$time_update = trim(strip_tags($time_update));
	$time_update = explode("/",$time_update);
	$time_update = mktime(0, 0, $time_update[1], $time_update[0], $time_update[2]);
	
	
	$thumb = $get_data_html->GetBetween($html_content, '<div class="screenshots">','</a>');
	$thumb = $get_data_html->GetBetween($thumb, 'src="','"');
	$thumb = trim(strip_tags($thumb));

	
	$icon = $get_data_html->GetBetween($html_content, '<h1 class="detail-title">','/>');
	$icon = $get_data_html->GetBetween($icon, 'src="','"');
	$icon = trim(strip_tags($icon));
	
	
	$version = $get_data_html->GetBetween($html_content, '<h1 class="detail-title">','</h1>');
	$version = $get_data_html->GetBetween($version, '<em>','</em>');
	$version = trim(strip_tags($version));
	
	
	$numdownload = $get_data_html->GetBetween($html_content, '<li class="downloads-info clearfix">','</li>');
	$numdownload = $get_data_html->GetBetween($numdownload, '<span class="item-info">','</span>');
	$numdownload = trim(strip_tags($numdownload));
	$numdownload = str_replace(array('.',','),'',$numdownload);
	
	
	$os = $get_data_html->GetBetween($html_content, '<li class="requirements-info clearfix">','</li>');
	$os = $get_data_html->GetBetween($os, '<span class="item-info">','</span>');
	$os = trim(strip_tags($os));
	
	
	$tags = get_meta_tags($link,true);
	
	
	$arr_insert['SGET_DATA'] = array();
	$arr_insert['SGET_DATA']['MAN_ID'] = 'http://www.download.com.vn'.$man;
	$arr_insert['SGET_DATA']['LIC_ID'] = $lic;
	$arr_insert['SGET_DATA']['CAT_ID'] = $cate;
	$arr_insert['SOFT_NAME_DESCRIPTION'] =$seo->DecimalToUni($name_desc);
	$arr_insert['SOFT_CONTENT'] = $seo->encode_tag_html($seo->DecimalToUni($detail));
	$arr_insert['SOFT_CONTENT_SHORT'] = null;
	$arr_insert['SOFT_FILESIZE'] = $size;
	//$arr_insert['SOFT_CODE'] = $seo->seolink();
	$arr_insert['SOFT_TIME_CREATE'] = $time_create;
	$arr_insert['SOFT_TIME_UPDATE'] = $time_update;
	$arr_insert['SOFT_THUMB'] = $thumb;
	$arr_insert['SOFT_ICON'] = $icon;
	$arr_insert['SOFT_VERSION'] = $version;
	$arr_insert['SOFT_DOWN'] = $numdownload;
	$arr_insert['SOFT_STATUS'] = 1;
	$arr_insert['SOFT_TIME_USE_LIMIT'] = null;
	$arr_insert['SOFT_REQUIREMENT_OS'] = $os;
	$arr_insert['SEO_KEYWORD'] = trim($tags['keywords']);
	$arr_insert['SEO_DESCRIPTION'] = trim($tags['description']);
	$arr_insert['GET_DATA'] = json_encode($arr_insert['SGET_DATA']);
	unset($arr_insert['SGET_DATA']);
	
	return $arr_insert;

	}
	function insert_man(){
	$path = 'E:\\man';
	$files = scandir($path);
	unset($files[0]);
	unset($files[1]);
 
	foreach($files as $file){
		parse_man($file);
		echo "\r".$file;
	}
	}
	function parse_man($link = ''){
	global $get_data_html;
	global $seo;
	
	$code = str_replace(".html","",$link);
	$link ='E:\man\\'.$code.'.html';
	//$link = 'E:\file\1379.html';
	//echo '<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />';
	$html_content = $get_data_html->get_content($link);
	
	$name = $get_data_html->GetBetween($html_content, '<div class="main-title">','</div>');	
	$name = $get_data_html->GetBetween($name, '<span>','</span>');
	$name = trim(strip_tags($name));
	
	$website = $get_data_html->GetBetween($html_content, '<li id="liWebsite">','</li>');	
	$website = $get_data_html->GetBetween($website, 'href="','"');
	$website = trim(strip_tags($website));

	$thumb = $get_data_html->GetBetween($html_content, 'id="profileIcon"','/>');	
	$thumb = $get_data_html->GetBetween($thumb, 'src="','"');
	$thumb = trim(strip_tags($thumb));
	
	$addr = $get_data_html->GetBetween($html_content, '<li id="liAddress">','</li>');	
	$addr = $get_data_html->GetBetween($addr, '<span id="lAddress">','</span>');
	$addr = trim(strip_tags($addr));
	
	$phone = $get_data_html->GetBetween($html_content, '<li id="liPhone">','</li>');	
	$phone = $get_data_html->GetBetween($phone, '<span id="lPhone">','</span>');
	$phone = trim(strip_tags($phone));
	
	$tags = get_meta_tags($link,true);
	$arr_insert = array();
	$arr_insert['MAN_NAME'] = $seo->DecimalToUni($name);
	$arr_insert['MAN_CODE'] = $seo->seolink($arr_insert['MAN_NAME']);
	$arr_insert['MAN_LINK'] = $website;
	$arr_insert['MAN_STATUS'] = 1;
	$arr_insert['MAN_THUMB'] = $thumb;
	$arr_insert['MAN_TIME_CREATE'] = time();
	$arr_insert['MAN_TIME_UPDATE'] = time();
	$arr_insert['MAN_ADDR'] = $seo->DecimalToUni($addr);
	$arr_insert['MAN_PHONE'] = $phone;
	//$arr_insert['MAN_COMPANY_NAME'] = $phone;
	$arr_insert['SEO_KEYWORD'] = trim(@$tags['keywords']);
	$arr_insert['SEO_DESCRIPTION'] = trim(@$tags['description']);
	$arr_insert['GET_CODE'] = $code;
	
	mysql_query("INSERT INTO manufacturer (MAN_NAME,MAN_CODE,MAN_LINK,MAN_STATUS,MAN_THUMB,MAN_TIME_CREATE,MAN_TIME_UPDATE,MAN_ADDR,MAN_PHONE,SEO_KEYWORD,SEO_DESCRIPTION,GET_CODE)
VALUES ('".$arr_insert['MAN_NAME']."','".$arr_insert['MAN_CODE']."','".$arr_insert['MAN_LINK']."','".$arr_insert['MAN_STATUS']."','".$arr_insert['MAN_THUMB']."','".$arr_insert['MAN_TIME_CREATE']."','".$arr_insert['MAN_TIME_UPDATE']."','".$arr_insert['MAN_ADDR']."','".$arr_insert['MAN_PHONE']."','".$arr_insert['SEO_KEYWORD']."','".$arr_insert['SEO_DESCRIPTION']."','".$arr_insert['GET_CODE']."')");

}
function download_man(){
$page = 0;
$limit = 20000;

$from = $page*$limit;
$to  = ($page+1)*$limit;

$result = mysql_query("SELECT `GET_ID`, `GET_LINK`,`GET_DATA` FROM `getsoft` where GET_STATUS = 2");

while($row = mysql_fetch_array($result))
{
  //echo "\r title:".$row['SOFT_NAME'];
  echo "\rGet ID: ".$row['GET_ID'];
  
	//{"MAN_ID":"http://www.download.com.vn/publisher/zdnet/index.aspx","LIC_ID":"trial","CAT_ID":"http://www.download.com.vn/security+firewall+tools/index.aspx"}
	$url = json_decode($row['GET_DATA']);
	if(!isset($url->MAN_ID) ||$url->MAN_ID =='' )
	{
		//mysql_query("insert into `error`  GET_STATUS = 1 where GET_ID = ".$row['GET_ID']);
		continue;
	
	} 
	$filename = 'E:\man\\'.md5($url->MAN_ID).'.html';

	if(file_exists($filename))
		continue;
	// Open the file to get existing content
	$current = file_get_contents($url->MAN_ID);
	
	// Write the contents back to the file
	file_put_contents($filename, $current);
	mysql_query("update `getsoft` set GET_STATUS = 3 where GET_ID = ".$row['GET_ID']);

}

mysql_close($con);


}

function insert_lic(){
$page = 0;
$limit = 20000;

$from = $page*$limit;
$to  = ($page+1)*$limit;

$result = mysql_query("SELECT `GET_DATA`,`GET_ID` FROM `getsoft`");
$arr_lic = array();
while($row = mysql_fetch_array($result))
{
  //echo "\r title:".$row['SOFT_NAME'];
  echo "\rGet ID: ".$row['GET_ID'];
  
	//{"MAN_ID":"http://www.download.com.vn/publisher/zdnet/index.aspx","LIC_ID":"trial","CAT_ID":"http://www.download.com.vn/security+firewall+tools/index.aspx"}
	$url = json_decode($row['GET_DATA']);
	if(!isset($url->LIC_ID) ||$url->LIC_ID =='' )
	{
 
		continue;
	
	} 
	if(!in_array($url->LIC_ID,$arr_lic)){
		$arr_lic[] = $url->LIC_ID;
	}
	//mysql_query("update `getsoft` set GET_STATUS = 3 where GET_ID = ".$row['GET_ID']);

}
foreach($arr_lic as $lic){
	mysql_query("INSERT INTO license (LICE_CODE)
VALUES ('".$lic."')");
echo "\rLicName: ".$lic;
}
mysql_close($con);
}
function update_cateid(){
$arr_lic = array();
$arr_cate = array();
$arr_man = array();
$result = mysql_query("SELECT GET_LINK,CAT_ID FROM `category`");
while($row = mysql_fetch_array($result))
{
	$t = md5($row['GET_LINK']);
	if(!in_array($t,$arr_cate)){
		//$arr_cate[] = $t;
		$arr_cate[$t] = $row['CAT_ID'];
	}
	echo "\rCate ID: ".$row['CAT_ID'];
}

$result = mysql_query("SELECT * FROM `manufacturer`");
while($row = mysql_fetch_array($result))
{

	$t = $row['GET_CODE'];
	if(!in_array($t,$arr_man)){
		//$arr_man[] = $t;
		$arr_man[$t] = $row['MAN_ID'];
	}
	echo "\rMan ID: ".$row['MAN_ID'];
}


$result = mysql_query("SELECT * FROM `license`");
while($row = mysql_fetch_array($result))
{
	$t = md5($row['LICE_CODE']);
	if(!in_array($t,$arr_lic)){
		//$arr_lic[] = $t;
		$arr_lic[$t] = $row['LICE_ID'];
	}
}

 
$result = mysql_query("SELECT `GET_DATA`,`GET_ID` FROM `getsoft`");

while($row = mysql_fetch_array($result))
{

	//{"MAN_ID":"http://www.download.com.vn/publisher/zdnet/index.aspx","LIC_ID":"trial","CAT_ID":"http://www.download.com.vn/security+firewall+tools/index.aspx"}
	$url = json_decode($row['GET_DATA']);
	
	$t = md5($url->MAN_ID);
	$t1 = md5($url->CAT_ID);
	$t2 = md5($url->LIC_ID);
	$manid = 0;
	$cateid = 0;
	$licid = 0;
	if(array_key_exists($t,$arr_man))
		$manid = $arr_man[$t];
		
	if(array_key_exists($t1,$arr_cate))
		$cateid = $arr_cate[$t1];
		
	if(array_key_exists($t2,$arr_lic))
		$licid = $arr_lic[$t2];
		
		echo "\rGet ID: ".$row['GET_ID'];
	//mysql_query
	mysql_query("update `getsoft` set GET_STATUS = 4,MAN_ID='".$manid."',LICE_ID='".$licid."', CAT_ID='".$cateid."'  where GET_ID = ".$row['GET_ID']);
	 
}
mysql_close($con);

}

function update_content_short(){
$result = mysql_query("SELECT `SOFT_NAME`,`GET_ID`,`SEO_DESCRIPTION` FROM `getsoft`");
while($row = mysql_fetch_array($result))
{
  //echo "\r title:".$row['SOFT_NAME'];
  echo "\rGet ID: ".$row['GET_ID'];
  if(!isset($row['SEO_DESCRIPTION']) || $row['SEO_DESCRIPTION'] =='' || $row['SEO_DESCRIPTION'] == NULL)
	continue;
	$row['SEO_DESCRIPTION'] = str_replace("Download ".$row['SOFT_NAME'].",","",$row['SEO_DESCRIPTION']);
	$row['SEO_DESCRIPTION'] = trim($row['SEO_DESCRIPTION']);
	mysql_query("update `getsoft` set GET_STATUS = 5,SOFT_CONTENT_SHORT ='".$row['SEO_DESCRIPTION']."' where GET_ID = '".$row['GET_ID']."'");
}
mysql_close($con);
}

function trans_post(){
global $seo;
$result = mysql_query("SELECT * FROM `getsoft`");
while($row = mysql_fetch_array($result))
{
  //echo "\r title:".$row['SOFT_NAME'];
  echo "\rGet ID: ".$row['GET_ID'];
$arr_insert = array();
//$arr_insert['MAN_ID'] = $row['MAN_ID'];
$arr_insert['LICE_ID'] = $row['LICE_ID'];
$arr_insert['CAT_ID'] = $row['CAT_ID'];
$arr_insert['SOFT_NAME'] = $row['SOFT_NAME'];
$arr_insert['SOFT_NAME_DESCRIPTION'] = $row['SOFT_NAME_DESCRIPTION'];
$arr_insert['SOFT_CONTENT'] = $row['SOFT_CONTENT'];
$arr_insert['SOFT_CONTENT_SHORT'] = $row['SOFT_CONTENT_SHORT'];
$arr_insert['SOFT_FILESIZE'] = $row['SOFT_FILESIZE'];
$arr_insert['SOFT_CODE'] = $seo->seolink($arr_insert['SOFT_NAME']);;
$arr_insert['SOFT_TIME_CREATE'] = $row['SOFT_TIME_CREATE'];
$arr_insert['SOFT_TIME_UPDATE'] = $row['SOFT_TIME_UPDATE'];
$arr_insert['SOFT_VIEW'] = $row['SOFT_VIEW'];
$arr_insert['SOFT_THUMB'] = $row['SOFT_THUMB'];
$arr_insert['SOFT_ICON'] = $row['SOFT_ICON'];
$arr_insert['SOFT_VERSION'] = $row['SOFT_VERSION'];
$arr_insert['SOFT_DOWN'] = $row['SOFT_DOWN'];
$arr_insert['SOFT_REQUIREMENT_OS'] = $row['SOFT_REQUIREMENT_OS'];
$arr_insert['SEO_H1'] = $row['SEO_H1'];
$arr_insert['SEO_KEYWORD'] = $row['SEO_KEYWORD'];
$arr_insert['SEO_DESCRIPTION'] = $row['SEO_DESCRIPTION'];
$arr_insert['SEO_METATITLE'] = $row['SEO_METATITLE'];

$str = "";
$str1 = "";
foreach($arr_insert as $k=>$v){
	$str.=",".$k;
	$str1.=",'".$v."'";
}

	if(mysql_query("INSERT INTO soft (MAN_ID".$str.")
VALUES ('".$row['MAN_ID']."' ".$str1.")"))
	continue;
}
mysql_close($con);

}

function update_cache(){
$arr_man = array();
$arr_cate = array();
$arr_lic = array();
$arr_images = array();

$result = mysql_query("SELECT * FROM `manufacturer`");
while($row = mysql_fetch_array($result,MYSQL_ASSOC))
{
	echo "\rMAN ID: ".$row['MAN_ID'];
	unset($row['SEO_H1']);
	unset($row['SEO_KEYWORD']);
	unset($row['SEO_DESCRIPTION']);
	unset($row['SEO_METATITLE']);
	unset($row['GET_CODE']);
	$arr_man[$row['MAN_ID']] = $row;
}

$result = mysql_query("SELECT * FROM `category` ");
while($row = mysql_fetch_array($result,MYSQL_ASSOC))
{
	echo "\rCAT ID: ".$row['CAT_ID'];
	unset($row['SEO_H1']);
	unset($row['SEO_KEYWORD']);
	unset($row['SEO_DESCRIPTION']);
	unset($row['SEO_METATITLE']);
	unset($row['GET_STATUS']);
	unset($row['GET_LINK']);
	unset($row['GET_LINK']);
	$arr_cate[$row['CAT_ID']] = $row;
}


$result = mysql_query("SELECT * FROM `license`");
while($row = mysql_fetch_array($result,MYSQL_ASSOC))
{
	$arr_lic[$row['LICE_ID']] = $row;
}
echo "\r ";
$result = mysql_query("SELECT SOFT_ID,MAN_ID,CAT_ID,LICE_ID FROM `soft`");
$arr_update = array();
while($row = mysql_fetch_array($result,MYSQL_ASSOC))
{
	echo "\rSOFT ID: ".$row['SOFT_ID'];
	
	$arr_update['SOFT_CACHE_MAN'] = json_encode($arr_man[$row['MAN_ID']]);
	$arr_update['SOFT_CACHE_CATEGORY'] = json_encode($arr_cate[$row['CAT_ID']]);
	$arr_update['SOFT_CACHE_LICENSE'] = json_encode($arr_lic[$row['LICE_ID']]);
	//$arr_update['SOFT_CACHE_IMAGES'] = json_encode(array());
	mysql_query("update `soft` set SOFT_CACHE_MAN = '".$arr_update['SOFT_CACHE_MAN']."',SOFT_CACHE_CATEGORY = '".$arr_update['SOFT_CACHE_CATEGORY']."',SOFT_CACHE_LICENSE = '".$arr_update['SOFT_CACHE_LICENSE']."' where SOFT_ID = '".$row['SOFT_ID']."'");
}


}

function fixCateID($link = ''){
	global $get_data_html;
	global $seo;
	//$link = 'E:\file\1379.html';
	//echo '<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />';
	$html_content = $get_data_html->get_content($link);
	
	$cate = $get_data_html->GetBetween($html_content, '<div class="breadcrumb">','</div>');
	$array = array('type' => '1',
				'estart' => '<span',
				'eend' => '<\/span>',
			);
	$cate = $get_data_html->load($cate, $array);
	
	//$cate = $get_data_html->GetBetween($html_content, '<span typeof="v:Breadcrumb">','</span>');
	$cate = end($cate);
	$cate = $get_data_html->GetBetween($cate, 'href="','"');
	$cate = str_replace(array('Dung lượng:','<span class="item-label">'),'',$cate);
	$cate = trim(strip_tags($cate));
	$cate = 'http://www.download.com.vn'.$cate;
	return $cate;
	
}


function fix_detail(){
$page = 0;
$limit = 10000;
global $seo;
$from = $page*$limit;
$to  = ($page+1)*$limit;
//$result = mysql_query("SELECT * FROM `getsoft` where GET_STATUS = 1 limit {$from},{$to}");
$result = mysql_query("SELECT GET_ID,GET_DATA,SOFT_NAME FROM `getsoft`");

while($row = mysql_fetch_array($result,MYSQL_ASSOC))
{
	echo "\rGet ID: ".$row['GET_ID'];
	$link = 'E:\file\\'.$row['GET_ID'].'.html';
	$arr_insert = array();

	
	$cateLink = fixCateID($link);
	$arr_insert['GET_DATA'] =array();
	$url = json_decode($row['GET_DATA']);

	$url->CAT_ID = $cateLink;
	$arr_insert['GET_DATA'] = json_encode($url);
	$arr_insert['SOFT_CODE'] = trim($seo->seolink($row['SOFT_NAME']));
	$arr_insert['SOFT_VIEW'] = rand(8000, 12000);
	mysql_query("UPDATE `getsoft` SET `GET_DATA` = '".$arr_insert['GET_DATA']."',`SOFT_CODE` ='".$arr_insert['SOFT_CODE']."',`SOFT_ID`='".$row['GET_ID']."',`SOFT_VIEW`='".$arr_insert['SOFT_VIEW']."'  WHERE `GET_ID` =  ".$row['GET_ID']."");

}

mysql_close($con);

}


function download_download(){
$page = 0;
$limit = 20000;
global $get_data_html;
$from = $page*$limit;
$to  = ($page+1)*$limit;

//$result = mysql_query("SELECT `GET_ID`, `GET_LINK`,`GET_DATA` FROM `getsoft`");

for($i =1; $i<63752; $i++){
  //echo "\r title:".$row['SOFT_NAME'];
  echo "\rGet ID: ".$i;
  $file_name = 'E:\file\\'.$i.'.html';
  $html_content = $get_data_html->get_content($file_name);
	
	$linkdownload = $get_data_html->GetBetween($html_content, '<div class="download-info top">','</div>');
	$linkdownload = $get_data_html->GetBetween($linkdownload, 'href="','"');
	$linkdownload = "http://www.download.com.vn".$linkdownload;
	
	if(!isset($linkdownload) || $linkdownload =='' )
	{
		mysql_query("insert into `error`  (ERR_TYPE,ERR_CONTENT) values ('GET_LINKDOWNLOAD', '".$i."')");
		continue;
	
	} 
	$filename = 'E:\linkdownload\\'.$i.'.html';

	if(file_exists($filename))
		continue;
	// Open the file to get existing content
	$current = file_get_contents($linkdownload);
	
	// Write the contents back to the file
	file_put_contents($filename, $current);
	//mysql_query("update `getsoft` set GET_STATUS = 3 where GET_ID = ".$row['GET_ID']);

}

mysql_close($con);


}

function parse_linkdownload(){
  global $get_data_html;
  //63752
	for($i =1; $i<63752; $i++){
  //echo "\r title:".$row['SOFT_NAME'];
	echo "\rGet ID: ".$i;
	$filename = 'E:\linkdownload\\'.$i.'.html';
	$html_content = $get_data_html->get_content($filename);
	
	$html_link = $get_data_html->GetBetween($html_content, '<div id="download_box" class="download-box">','<div class="clear">');
	$html_link = trim(strip_tags($html_link,'<a>,<span>'));
	$array = array('type' => '1',
				'estart' => '<a',
				'eend' => '<\/a>',
			);
	$links = $get_data_html->load($html_link, $array);
	foreach($links as $link){
	$arr_insert = array();
		$href = $get_data_html->GetBetween($link, 'href="','"');
		$href= trim($href);
		$arr_insert['SOFT_ID'] = $i;
		$arr_insert['LINK_NAME'] = $href;
		
		$name = $get_data_html->GetBetween($link, '<span class="link-title">','</span>');
		$name = trim(strip_tags($name));
		$arr_insert['LINK_LINKDOWN'] = $href;
		
		mysql_query("INSERT INTO `linkdown` (SOFT_ID,LINK_NAME,LINK_LINKDOWN)
VALUES ('".$arr_insert['SOFT_ID']."','".$arr_insert['LINK_NAME']."','".$arr_insert['LINK_LINKDOWN']."')");
	}
  }

}

function parse_tag(){
	global $get_data_html;
	global $seo;
	for($i =1; $i<63752; $i++){
  //echo "\r title:".$row['SOFT_NAME'];
	echo "\rGet ID: ".$i;
	$filename = 'E:\file\\'.$i.'.html';
	$html_content = $get_data_html->get_content($filename);
	
	$html_link = $get_data_html->GetBetween($html_content, "<div class='tags'>",'</div>');
	$html_link = trim(strip_tags($html_link,'<a>'));
	$array = array('type' => '1',
				'estart' => '<a',
				'eend' => '<\/a>',
			);
	$links = $get_data_html->load($html_link, $array);
	foreach($links as $link){
	$arr_insert = array();
		$tagname = $get_data_html->GetBetween($link, '">','"');
		$tagname= trim($tagname);
		$tagname= trim(strip_tags($tagname));
		if($tagname =='')
			continue;
		$arr_insert['SOFT_ID'] = $i;
		$arr_insert['TAG_NAME'] = $seo->DecimalToUni($tagname);
		
		mysql_query("INSERT INTO `soft_tag` (SOFT_ID,TAG_NAME)
VALUES ('".$arr_insert['SOFT_ID']."','".$arr_insert['TAG_NAME']."')");
	}
//sleep(9999999999999);
  }

}

function check_code(){


}

function parse_images_step1(){
	global $get_data_html;
	global $seo;
	
 
	//category
	$result = mysql_query("select * from category where CAT_THUMB !=''");
	$arr_insert = array();
	while($row = mysql_fetch_array($result,MYSQL_ASSOC)){
		$arr_insert['GETI_CODE'] = 'category.CAT_THUMB.'.$row['CAT_ID'];
		$arr_insert['GETI_LINK_OLD'] = $row['CAT_THUMB'];
		$arr_insert['GETI_NAME_NEW'] = $row['CAT_CODE'];
		mysql_query("INSERT INTO `getimages` (GETI_CODE,GETI_LINK_OLD,GETI_NAME_NEW)
VALUES ('".$arr_insert['GETI_CODE']."','".$arr_insert['GETI_LINK_OLD']."','".$arr_insert['GETI_NAME_NEW']."')");
	}
 
	
	//soft
	$result = mysql_query("select * from soft where SOFT_ID = 2");
	$arr_insert = array();
	while($row = mysql_fetch_array($result,MYSQL_ASSOC)){
		$arr_insert['GETI_CODE'] = 'soft.SOFT_THUMB.'.$row['SOFT_ID'];
		$arr_insert['GETI_LINK_OLD'] = $row['SOFT_THUMB'];
		$arr_insert['GETI_NAME_NEW'] = $row['SOFT_CODE'];
		mysql_query("INSERT INTO `getimages` (GETI_CODE,GETI_LINK_OLD,GETI_NAME_NEW)
VALUES ('".$arr_insert['GETI_CODE']."','".$arr_insert['GETI_LINK_OLD']."','".$arr_insert['GETI_NAME_NEW']."')");

	$arr_insert['GETI_CODE'] = 'soft.SOFT_ICON.'.$row['SOFT_ID'];
		$arr_insert['GETI_LINK_OLD'] = $row['SOFT_ICON'];
		$arr_insert['GETI_NAME_NEW'] = $row['SOFT_CODE'];
		mysql_query("INSERT INTO `getimages` (GETI_CODE,GETI_LINK_OLD,GETI_NAME_NEW)
VALUES ('".$arr_insert['GETI_CODE']."','".$arr_insert['GETI_LINK_OLD']."','".$arr_insert['GETI_NAME_NEW']."')");




	//get SOFT_CONTENT_SHORT
	$content =  $seo->decode_tag_html($row['SOFT_CONTENT_SHORT']);
	$content = trim(strip_tags($content,'<img>'));
	$array = array('type' => '1',
				'estart' => 'src="',
				'eend' => '"',
			);
	$images = $get_data_html->load($content, $array);
	
	$array = array('type' => '1',
				'estart' => "src='",
				'eend' => "'",
			);
	$images2 = $get_data_html->load($content, $array);
	foreach($images2 as $r){
		$images[] = $r;
	}
	if(count($images)>0)
	foreach($images as $image){
	if($image == '')
		continue;
	$arr_insert['GETI_CODE'] = 'soft.SOFT_CONTENT_SHORT.'.$row['SOFT_ID'];
		$arr_insert['GETI_LINK_OLD'] = $image;
		$arr_insert['GETI_NAME_NEW'] = $row['SOFT_CODE'];
		mysql_query("INSERT INTO `getimages` (GETI_CODE,GETI_LINK_OLD,GETI_NAME_NEW)
		VALUES ('".$arr_insert['GETI_CODE']."','".$arr_insert['GETI_LINK_OLD']."','".$arr_insert['GETI_NAME_NEW']."')");
	}
	
	
	
 
	//get SOFT_CONTENT
	$content =  $seo->decode_tag_html($row['SOFT_CONTENT']);
	$content = trim(strip_tags($content,'<img>'));
	$array = array('type' => '1',
				'estart' => 'src="',
				'eend' => '"',
			);
	$images = $get_data_html->load($content, $array);
	
	$array = array('type' => '1',
				'estart' => "src='",
				'eend' => "'",
			);
	$images2 = $get_data_html->load($content, $array);
	foreach($images2 as $r){
		$images[] = $r;
	}
	if(count($images)>0)
	foreach($images as $image){
	if($image == '')
		continue;
	$arr_insert['GETI_CODE'] = 'soft.SOFT_CONTENT.'.$row['SOFT_ID'];
		$arr_insert['GETI_LINK_OLD'] = $image;
		$arr_insert['GETI_NAME_NEW'] = $row['SOFT_CODE'];
		mysql_query("INSERT INTO `getimages` (GETI_CODE,GETI_LINK_OLD,GETI_NAME_NEW)
		VALUES ('".$arr_insert['GETI_CODE']."','".$arr_insert['GETI_LINK_OLD']."','".$arr_insert['GETI_NAME_NEW']."')");
	}
 
	
	}

}



function trans_tag(){
	
$result = mysql_query("SELECT * FROM `soft_tag`");
while($row = mysql_fetch_array($result))
{

	$tagname = $row['TAG_NAME'];
	$check = mysql_query("SELECT * FROM `tag` where TAG_NAME ='".$tagname."'");
	if($t = mysql_fetch_array($check))
		continue;
		
	mysql_query("INSERT INTO `tag` (TAG_NAME)
VALUES ('".$tagname."')");
$id = mysql_insert_id();
mysql_query("update `soft_tag` set TAG_ID = '".$id."' where SOFTTAG_ID = '".$row['SOFTTAG_ID']."'");
	echo "\rInsert ID: ".$id;
}

mysql_close($con);
}

function fix_tag(){
	$arr =array();
	$result = mysql_query("SELECT * FROM `tag`");
	//$result = mysql_query("SELECT * FROM `soft_tag` where TAG_ID is null");
	while($row = mysql_fetch_array($result))
	{
		mysql_query("update `soft_tag` set TAG_ID = '".$row['TAG_ID']."' where TAG_NAME = '".$row['TAG_NAME']."'");
		echo "\rAdd ID: ".$row['TAG_ID'];

	}
	/*
	$result2 = mysql_query("SELECT * FROM `soft_tag`");
	while($row2 = mysql_fetch_array($result2))
	{
		if(!$arr[md5($row2['TAG_NAME'])]){
		print_r($row2['TAG_NAME']);
		sleep(9999999);
		}
		$tagID = $arr[md5($row2['TAG_NAME'])];
			
		echo "\Tag ID: ".$tagID;
		//mysql_query("update `soft_tag` set TAG_ID = '".$tagID."' where SOFTTAG_ID = '".$row2['SOFTTAG_ID']."'");
	}
	*/
}

function puts_download_image_file(){
	global $get_data_html ;
	global $seo ;
	$result = mysql_query("SELECT * FROM `getimages`");


	while($row = mysql_fetch_array($result))
	{
		echo "\rGet ID: ".$row['GETI_ID'];
  
	$file = $row['GETI_LINK_OLD'];
	$type = explode(".",$row['GETI_CODE']);
	// Open the file to get existing content
	$current = file_get_contents($file);
	if(!$current)
		continue;
	$filename = basename($file);
	$ext = pathinfo($filename, PATHINFO_EXTENSION);

	if($type[0] == "category"){
		$folder = "category";
		$name = $row['GETI_NAME_NEW']."-thumb.".$ext;
	}
	else if($type[0] == "manufacturer"){
		$folder = "manufacturer";
		$name = $row['GETI_NAME_NEW']."-thumb.".$ext;
	
	}
	else if($type[0] == "soft"){
		$folder = "soft";
		if($type[1] == "SOFT_THUMB") {
			$folder = "soft/thumb";
			$name = $row['GETI_NAME_NEW'].'-'.$type[2]."-thumb.".$ext;
		}
		else if ($type[1] == "SOFT_ICON") {
			$folder = "soft/icon";
			$name = $row['GETI_NAME_NEW'].'-'.$type[2]."-icon.".$ext;
		}else if ($type[1] == "SOFT_CONTENT_SHORT" || $type[1] == "SOFT_CONTENT") {
			$folder = "soft/images";
			$name = $row['GETI_NAME_NEW'].'-'.$type[2].".".$ext;
			if(file_exists(dirname(__FILE__).'/'.'dataupload/soft/images/'.$name)){
				for($i=1; $i<1000; $i++){
					$name = $row['GETI_NAME_NEW'].'-'.$type[2].'-'.$i.".".$ext;
					if(!file_exists(dirname(__FILE__).'/'.'dataupload/soft/images/'.$name))
						break;
				}
			}
		}
	}
	
	$mpatch = $folder."/".$name;
	$patch = 'dataupload/'.$mpatch;
	$filex = dirname(__FILE__).'/'.$patch;
	file_put_contents($filex, $current);
	mysql_query("update `getimages` set GETI_LINK_NEW = '".$filex."' where GETI_ID = '".$row['GETI_ID']."'");
	}

}

function update_content(){
$result = mysql_query("SELECT * FROM `getimages`");
global $seo ;
	while($row = mysql_fetch_array($result))
	{
		echo "\rGet ID: ".$row['GETI_ID'];
		if(!$row['GETI_LINK_NEW'] || $row['GETI_LINK_NEW'] =='')
			continue;
		$type = explode(".",$row['GETI_CODE']);
		$newpatch = str_replace("/home/admin/mail5.mailercenter.com/","",$row['GETI_LINK_NEW']);
		if($type[0] == "category"){
			//mysql_query("update `category` set CAT_THUMB = '".$newpatch."' where CAT_ID = '".$type[2]."'");
			continue;
		}
		else if($type[0] == "manufacturer"){
			//mysql_query("update `manufacturer` set MAN_THUMB = '".$newpatch."' where MAN_ID = '".$type[2]."'");
			continue;
		
		}else if($type[0] == "soft"){
			if($type[1] =="SOFT_THUMB"){
				mysql_query("update `soft` set SOFT_THUMB = '".$newpatch."' where SOFT_ID = '".$type[2]."'");
					continue;
			}
			else if($type[1] =="SOFT_ICON"){
				mysql_query("update `soft` set SOFT_ICON = '".$newpatch."' where SOFT_ID = '".$type[2]."'");
					continue;
			}
			else {
			
			$check = mysql_query("SELECT * FROM `soft` where SOFT_ID ='".$type[2]."'");
			if(!$t = mysql_fetch_array($check))
				continue;
			
			$content = $seo->encode_tag_html($t['SOFT_CONTENT']);
			$content = str_replace($row['GETI_LINK_OLD'],$newpatch,$content);
			
			mysql_query("update `soft` set SOFT_CONTENT = '".$content."' where SOFT_ID = '".$type[2]."'");
			mysql_query("INSERT INTO soft_images (SOFT_ID,SOFT_PATCH) VALUES ('".$type[2]."','".$newpatch."')");
			continue;
			}
		}
	}
}
?>